import { ILoadMapTypeAll, ISearchTranslateAll } from '@/common';
import { ManageCommonAPI } from '@/pages/type';
import { getBaseUrl } from '@/services/urlconst';
import { commonManageSearchParams, getTenantID, getUserID, handleCheckSearchParams, requestFunComponFiex } from '@/utils';
import { useMemo } from 'react';
import { useRequest } from 'umi';
import { HomeTaskTabSwitch, getHomeSearchList, getHomeTabSwitch, homeTaskSearchConfig } from '../mconfig';
import { ApplyManageTab, AssetMaterialManage, DepartmentTab, getUser } from '../mconfig/ManageTabSwitch';

const baseUrl = getBaseUrl();
// const baseUrl = 'KLapi';

const contractHomeService = (curRes: Record<string, any>, callback: (res: any) => void) => {
  /**  需要获取的数据字典 */
  const loadMdicIds: string[] = [
    `${getTenantID()}${ILoadMapTypeAll.PURCHASE_CATEGORY}`, //采购类别
    `${getTenantID()}${ILoadMapTypeAll.CONTRACT_TYPE}`, //采购类型
    `${getTenantID()}${ILoadMapTypeAll.PURCHASE_METHOD}`, //采购方式
    `${getTenantID()}${ILoadMapTypeAll.CONTRACT_STATE}`, //合同状态
    `${getTenantID()}${ILoadMapTypeAll.PAYMENT_STYLE}`, //支付方式
  ];

  /**获取权限参数 */
  const queryTypeObj: Record<string, any> = getHomeTabSwitch(curRes);
  // console.log(queryTypeObj)
  /** 获取搜索参数 */
  const searchListParams = getHomeSearchList(queryTypeObj);

  /** 获取列表数据  */
  const { loading, run, refresh } = useRequest(
    (opt: ManageCommonAPI.IGridRequestType) => {
      const requestBody: Record<string, any> = {
        ...queryTypeObj,
        current: 1,
        pageSize: 10,
        sort: 'createTime',
        order: 'desc',
        dicTypeIds: loadMdicIds?.toString(),
        ...opt,
      };
      const searchParams: Record<string, any> = commonManageSearchParams(searchListParams, requestBody, {
        dicTransName: 'supplyKind,supplyType,procureReal,changeTypeName,state,payStyle',
        orgTransName: `${ISearchTranslateAll.SIGN_DEPT},${ISearchTranslateAll.CREATER_DEPT},${ISearchTranslateAll.OWNER_DEPT},${ISearchTranslateAll.CANCEL_DEPT}`,
        // areaTransName: `${ISearchTranslateAll.AREA_TRANS}`,
      });

      const requestURL = `${baseUrl}/contractController/dataGrid/${requestBody?.params}`;
      return requestFunComponFiex('POST', requestURL, searchParams);
    },
    {
      manual: true, //页面初始请求为 true
      paginated: true,
      defaultPageSize: 10,
      onSuccess: callback,
    },
  );

  /* 获取页面数据 */
  const getContractData = (otherRequest: ManageCommonAPI.IGridRequestType | any) => {
    const request: ManageCommonAPI.IGridRequestType | any = {
      ...otherRequest,
    };
    if (request?.createTime) {
      const [createTimeStart, createTimeEnd] = request.createTime;
      request['createTimeStart'] = createTimeStart.format('YYYY-MM-DD 00:00:00');
      request['createTimeEnd'] = createTimeEnd.format('YYYY-MM-DD 23:59:59');
      delete request['createTime'];
    }

    if (request?.belongArea) {
      request['provinceCode'] = request.belongArea[0];
      request['cityCode'] = request.belongArea[1];
      request['countyCode'] = request.belongArea[2];
      delete request['belongArea'];
    }

    delete request['applyFormNameDept'];
    delete request['applyFormNameUser'];
    if (request?.createrID && request?.createrID?.length > 0) {
      request['createrID'] = request?.createrID?.toString();
    } else {
      delete request['createrID'];
    }

    run({
      ...request,
    });
  };

  return {
    loading,
    getContractData,
    reloadRefresh: refresh,
  };
};

/**
 * 管理页面处理数据
 * @param curRes  权限参数
 */
export const taskPushService = (callback) => {
  const request: any = useRequest(
    (payload: any) => {
      let reqParams = {
        current: 1,
        pageSize: 10,
        sysCode: 'ZYKYPT-SXSQ-008',
        receiver: getUserID(),
        sort: 'receiveDateTime',
        ...payload,
      };
      let reqUrl = `${baseUrl}/taskPushController/dataGrid`;
      return requestFunComponFiex('POST', reqUrl, reqParams);
    },
    {
      manual: true,
      paginated: true,
      defaultPageSize: 10,
      onSuccess: callback,
    },
  );
  return {
    taskPushRun: request.run,
    taskPushLoading: request.loading,
    taskPushPagination: request.pagination,
  };
};

/**
 * 获取详情
 * @param callback  获取详情回调
 * @returns
 */
const getMcontractByIDService = (callback: (res: any) => void) => {
  /**  需要获取的数据字典 */
  const loadMdicIds: string[] = [
    `${getTenantID()}${ILoadMapTypeAll.PURCHASE_CATEGORY}`, //采购类别
    `${getTenantID()}${ILoadMapTypeAll.CONTRACT_TYPE}`, //采购类型
    `${getTenantID()}${ILoadMapTypeAll.PURCHASE_METHOD}`, //采购方式
    `${getTenantID()}${ILoadMapTypeAll.CONTRACT_STATE}`, //合同状态
    `${getTenantID()}${ILoadMapTypeAll.PAYMENT_STYLE}`, //支付方式
    `${getTenantID()}${ILoadMapTypeAll.COMPANY_TAXRATE}`, //税率
    `${getTenantID()}${ILoadMapTypeAll.CONTRACT_FIELDTYPE}`, //合同附件类型
    `${getTenantID()}${ILoadMapTypeAll.MATERIAL_MIDI}`, //量化单位
    `${getTenantID()}${ILoadMapTypeAll.ALLORDER_TYPE}`, //工单类型
  ];

  const getMcontractByID = useRequest(
    (params: any) => {
      let personUrl = `${baseUrl}/contractController/getMcontractByID`;
      const payload: Record<string, any> = {
        ...params,
        dicTypeIds: loadMdicIds?.toString(),
        dicTransName: 'state,supplyKind,supplyType,procureReal,payStyle,businessType,checkState,orderType',
        orgTransName: `${ISearchTranslateAll.CREATER_DEPT},${ISearchTranslateAll.OWNER_DEPT},${ISearchTranslateAll.CANCEL_DEPT},${ISearchTranslateAll.DUTY_DEPT}`,
        currentUserID: getUserID(),
      };
      return requestFunComponFiex('PUT', personUrl, payload);
    },
    {
      manual: true,
      onSuccess: callback,
    },
  );

  return {
    getMcontractByIDRun: getMcontractByID?.run,
    getMcontractByIDLoading: getMcontractByID?.loading,
    getMcontractByIDInfo: getMcontractByID?.data?.obj,
  };
};

// 资产状态占比
const getAssetproportion = (curRes) => {
  /**获取权限参数 */
  const queryTypeObj: Record<string, any> = ApplyManageTab(curRes);

  const { tableProps, data, loading, run, refresh } = useRequest(
    (opt: ManageCommonAPI.IGridRequestType) => {
      const requestBody: Record<string, any> = {
        ...queryTypeObj,
      };
      const requestURL = `${baseUrl}/assetCount/assetStateRatio/${queryTypeObj.params}`;
      return requestFunComponFiex('POST', requestURL, requestBody);
    },
    {
      manual: false, //页面初始请求为 true
      paginated: true,
      defaultPageSize: 10,
    },
  );

  /* 获取页面数据 */
  const getWarnData = (otherRequest: ManageCommonAPI.IGridRequestType | any) => {
    const request: ManageCommonAPI.IGridRequestType | any = {
      ...otherRequest,
    };

    run({
      ...request,
    });
  };

  /** 页面列表 */
  const returnTableProps = useMemo(() => {
    return {
      Assetproportiondata: data,
      pagination: tableProps?.pagination,
    };
  }, [data, tableProps]);

  return {
    loading,
    getWarnData,
    reloadRefresh: refresh,
    ...returnTableProps,
  };
};

// 资产分类统计
const getDepartmentalList = (curRes) => {
  /**获取权限参数 */
  const queryTypeObj: Record<string, any> = ApplyManageTab(curRes);

  const { tableProps, data, loading, run, refresh } = useRequest(
    (opt: ManageCommonAPI.IGridRequestType) => {
      const requestBody: Record<string, any> = {
        ...queryTypeObj,
      };
      const requestURL = `${baseUrl}/assetCount/materialClassifyRatio/${queryTypeObj.params}`;
      return requestFunComponFiex('POST', requestURL, requestBody);
    },
    {
      manual: false, //页面初始请求为 true
      paginated: true,
      defaultPageSize: 10,
    },
  );

  /* 获取页面数据 */
  const getWarnData = (otherRequest: ManageCommonAPI.IGridRequestType | any) => {
    const request: ManageCommonAPI.IGridRequestType | any = {
      ...otherRequest,
    };

    run({
      ...request,
    });
  };

  /** 页面列表 */
  const returnTableProps = useMemo(() => {
    return {
      DepartmentalData: data,
      pagination: tableProps?.pagination,
    };
  }, [data, tableProps]);

  return {
    loading,
    getWarnData,
    reloadRefresh: refresh,
    ...returnTableProps,
  };
};

// 资产分类统计
const getAssetclassList = (curRes) => {
  /**获取权限参数 */
  const queryTypeObj: Record<string, any> = ApplyManageTab(curRes);
  /** 获取列表数据  */
  const { tableProps, data, loading, run, refresh } = useRequest(
    (opt: ManageCommonAPI.IGridRequestType) => {
      const requestBody: Record<string, any> = {
        ...queryTypeObj,
      };
      const requestURL = `${baseUrl}/assetCount/classifyRatio/${queryTypeObj.params}`;
      return requestFunComponFiex('POST', requestURL, requestBody);
    },
    {
      manual: false, //页面初始请求为 true
      paginated: true,
      defaultPageSize: 10,
    },
  );

  /* 获取页面数据 */
  const getWarnData = (otherRequest: ManageCommonAPI.IGridRequestType | any) => {
    const request: ManageCommonAPI.IGridRequestType | any = {
      ...otherRequest,
    };

    run({
      ...request,
    });
  };

  /** 页面列表 */
  const returnTableProps = useMemo(() => {
    return {
      AssetclassData: data,
      pagination: tableProps?.pagination,
    };
  }, [data, tableProps]);

  return {
    loading,
    getWarnData,
    reloadRefresh: refresh,
    ...returnTableProps,
  };
};

// 资产使用情况
const getAssetusageList = (curRes) => {
  /**获取权限参数 */
  const queryTypeObj: Record<string, any> = ApplyManageTab(curRes);
  /** 获取列表数据  */
  const { tableProps, data, loading, run, refresh } = useRequest(
    (opt: ManageCommonAPI.IGridRequestType) => {
      const requestBody: Record<string, any> = {
        ...queryTypeObj,
      };
      const requestURL = `${baseUrl}/assetCount/assetUseRatio/${queryTypeObj.params}`;
      return requestFunComponFiex('POST', requestURL, requestBody);
    },
    {
      manual: false, //页面初始请求为 true
      paginated: true,
      defaultPageSize: 10,
    },
  );

  /* 获取页面数据 */
  const getWarnData = (otherRequest: ManageCommonAPI.IGridRequestType | any) => {
    const request: ManageCommonAPI.IGridRequestType | any = {
      ...otherRequest,
    };

    run({
      ...request,
    });
  };

  /** 页面列表 */
  const returnTableProps = useMemo(() => {
    return {
      AssetusageData: data,
      pagination: tableProps?.pagination,
    };
  }, [data, tableProps]);

  return {
    loading,
    getWarnData,
    reloadRefresh: refresh,
    ...returnTableProps,
  };
};

// 耗材领用情况
const getConsumableList = (curRes) => {
  /**获取权限参数 */
  const queryTypeObj: Record<string, any> = ApplyManageTab(curRes);
  /** 获取列表数据  */
  const { tableProps, data, loading, run, refresh } = useRequest(
    (opt: ManageCommonAPI.IGridRequestType) => {
      const requestBody: Record<string, any> = {
        ...queryTypeObj,
      };
      const requestURL = `${baseUrl}/assetCount/materialUseRatio/${queryTypeObj.params}`;
      return requestFunComponFiex('POST', requestURL, requestBody);
    },
    {
      manual: false, //页面初始请求为 true
      paginated: true,
      defaultPageSize: 10,
    },
  );

  /* 获取页面数据 */
  const getWarnData = (otherRequest: ManageCommonAPI.IGridRequestType | any) => {
    const request: ManageCommonAPI.IGridRequestType | any = {
      ...otherRequest,
    };

    run({
      ...request,
    });
  };

  /** 页面列表 */
  const returnTableProps = useMemo(() => {
    return {
      ConsumableData: data,
      pagination: tableProps?.pagination,
    };
  }, [data, tableProps]);

  return {
    loading,
    getWarnData,
    reloadRefresh: refresh,
    ...returnTableProps,
  };
};

// 资产区域统计
const getDeparconsumList = (curRes) => {
  /**获取权限参数 */
  const queryTypeObj: Record<string, any> = ApplyManageTab(curRes);
  /** 获取列表数据  */
  const { tableProps, data, loading, run, refresh } = useRequest(
    (opt: ManageCommonAPI.IGridRequestType) => {
      const requestBody: Record<string, any> = {
        ...queryTypeObj,
      };
      const requestURL = `${baseUrl}/assetCount/assetLocationCount/${queryTypeObj.params}`;
      return requestFunComponFiex('POST', requestURL, requestBody);
    },
    {
      manual: false, //页面初始请求为 true
      paginated: true,
      defaultPageSize: 10,
    },
  );

  /* 获取页面数据 */
  const getWarnData = (otherRequest: ManageCommonAPI.IGridRequestType | any) => {
    const request: ManageCommonAPI.IGridRequestType | any = {
      ...otherRequest,
    };

    run({
      ...request,
    });
  };

  /** 页面列表 */
  const returnTableProps = useMemo(() => {
    return {
      DeparconsumData: data,
      pagination: tableProps?.pagination,
    };
  }, [data, tableProps]);

  return {
    loading,
    getWarnData,
    reloadRefresh: refresh,
    ...returnTableProps,
  };
};

// 资产数
const getAssetsNumber = (curRes) => {
  /**获取权限参数 */
  const queryTypeObj: Record<string, any> = ApplyManageTab(curRes);
  /** 获取列表数据  */
  const { tableProps, data, loading, run, refresh } = useRequest(
    (opt: ManageCommonAPI.IGridRequestType) => {
      const requestBody: Record<string, any> = {
        ...queryTypeObj,
      };
      const requestURL = `${baseUrl}/assetCount/assetCountNum/${queryTypeObj.params}`;
      return requestFunComponFiex('POST', requestURL, requestBody);
    },
    {
      manual: false, //页面初始请求为 true
      paginated: true,
      defaultPageSize: 10,
    },
  );

  /* 获取页面数据 */
  const getWarnData = (otherRequest: ManageCommonAPI.IGridRequestType | any) => {
    const request: ManageCommonAPI.IGridRequestType | any = {
      ...otherRequest,
    };

    run({
      ...request,
    });
  };

  /** 页面列表 */
  const returnTableProps = useMemo(() => {
    return {
      AssetsNumber: data,
      pagination: tableProps?.pagination,
    };
  }, [data, tableProps]);

  return {
    loading,
    getWarnData,
    reloadRefresh: refresh,
    ...returnTableProps,
  };
};

export {
  contractHomeService,
  getMcontractByIDService,
  getAssetproportion,
  getDepartmentalList,
  getAssetclassList,
  getAssetusageList,
  getConsumableList,
  getDeparconsumList,
  getAssetsNumber,
};
